home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / gport.exe / GAMDEMO.C < prev    next >
C/C++ Source or Header  |  1991-08-30  |  19KB  |  737 lines

  1. /*
  2. // GAMDEMO.C
  3. //
  4. // Game port demo program for the Gport game port library.
  5. //
  6. // Compiled and linked with Borland C++ v2.00
  7. //
  8. // Copyright (c) 1991 Bri Productions
  9. //
  10. */
  11.  
  12.  
  13. #include "gport.h"
  14. #include <stdlib.h>
  15. #include <bios.h>
  16. #include <graphics.h>
  17. #include <alloc.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <dos.h>
  21.  
  22.  
  23. #define  AXIS  0                    /* GamAxis()/GamStick switch  */
  24.  
  25. #define  limit(n,max,min)  (n) > (max) ? (max) : ((n) < (min) ? (min) :(n))
  26.  
  27.  
  28. /*
  29. //-------------------------------------
  30. //
  31. // screen coordinates, sizes, etc.
  32. //
  33. //-------------------------------------
  34. */
  35.  
  36. #define  BASE_X      640            /* base with of the screen */
  37. #define  BASE_Y      480            /* base with of the screen */
  38. #define  XREF        9              /* X scaling reference     */
  39. #define  YREF        35             /* X scaling reference     */
  40. #define  OAX         170            /* x origin of joystick a  */
  41. #define  OAY         175            /* y origin of joystick a  */
  42. #define  OBX         470            /* x origin of joystick b  */
  43. #define  OBY         175            /* y origin of joystick b  */
  44. #define  JR          135            /* joystick radius         */
  45. #define  TSIZE       5              /* size of tic marks       */
  46. #define  TSPACE      10             /* spacing of tic marks    */
  47. #define  TNUM        13             /* number of tic marks     */
  48. #define  HSIZE       5              /* cross hair size         */
  49. #define  BAY         350            /* y center of a buttons   */
  50. #define  BBY         350            /* y center of b buttons   */
  51. #define  BSPACE      100            /* x space between buttons */
  52. #define  BR          20             /* button radius           */
  53. #define  BON         1              /* button on               */
  54. #define  BOFF        0              /* button off              */
  55. #define  GAIN        7              /* gain divisor for Gport  */
  56. #define  BIOS_GAIN   14             /* gain multiplier for bios*/
  57.  
  58. #define  Y_COORD_OFF 235            /* Y coordinate offset     */
  59. #define  JAY_COORD   (OAY) + Y_COORD_OFF
  60. #define  JBY_COORD   (OAY) + Y_COORD_OFF
  61. #define  TEXT_Y   370               /* text y position         */
  62.  
  63.  
  64. /*
  65. //-------------------------------------
  66. //
  67. // Text justification
  68. //
  69. //-------------------------------------
  70. */
  71.  
  72. #define  J_LEFT      0              /* left justified text     */
  73. #define  J_CENTER    1              /* center justified text   */
  74. #define  J_RIGHT     2              /* right justified text    */
  75.  
  76. /*
  77. //-------------------------------------
  78. //
  79. // keystrokes
  80. //
  81. //-------------------------------------
  82. */
  83.  
  84. #define  F1    0x3B00
  85. #define  F2    0x3C00
  86. #define  F3    0x3D00
  87. #define  F4    0x3E00
  88. #define  F5    0x3F00
  89. #define  F6    0x4000
  90. #define  F7    0x4100
  91. #define  F8    0x4200
  92. #define  F9    0x4300
  93. #define  F10   0x4400
  94. #define  ESC   0x011B
  95.  
  96.  
  97. /*
  98. //-------------------------------------
  99. //
  100. // function prototypes
  101. //
  102. //-------------------------------------
  103. */
  104.  
  105. void  Init        (void);
  106. void  Uninit      (void);
  107. void  PutAxis     (int x, int y);
  108. void  PutButton   (int x, int off);
  109. void  CrossHair   (int x, int y);
  110. void  Button      (byte status);
  111. void  PutCoord    (int x, int y, int just, signed value);
  112. void  gputs       (int x, int y, int color, int just, char *str);
  113. void  gerase      (int x, int y, int color, int just, char *str);
  114.  
  115.  
  116. /*
  117. //-------------------------------------
  118. //
  119. // global variables
  120. //
  121. //-------------------------------------
  122. */
  123.  
  124. void  *old_image[2];             /* image behind the cross hair   */
  125. int   Xasp, Yasp;                /* X and Y aspect ratios         */
  126. int   bios_ax0, bios_ay0;        /* bios values for joystick A    */
  127. int   bios_bx0, bios_by0;        /* bios values for joystick B    */
  128.  
  129.  
  130. /*
  131. //-------------------------------------
  132. //
  133. // default colors (vga/ega)
  134. //
  135. //-------------------------------------
  136. */
  137.  
  138. int   color   = YELLOW;          /* default color     */
  139. int   bcolor  = RED;             /* button color      */
  140. int   vcolor  = YELLOW;          /* values color      */
  141. int   tcolor  = GREEN;           /* text color        */
  142. int   hcolor  = WHITE;           /* cross hair color  */
  143. int   bkcolor = DARKGRAY;        /* background color  */
  144.  
  145.  
  146. /*
  147. //-------------------------------------
  148. //
  149. // function macros
  150. //
  151. //-------------------------------------
  152. */
  153.  
  154. #define  X(a)  (a)*XREF/Xasp     /* X scaling   */
  155. #define  Y(a)  (a)*YREF/Yasp     /* Y scaling   */
  156.  
  157.  
  158. /*
  159. //-------------------------------------
  160. //
  161. // main()
  162. //
  163. //-------------------------------------
  164. */
  165.  
  166. void main(void)
  167. {
  168. signed ax, ay, bx, by;                 /* joystick coordinates       */
  169. signed old_ax, old_ay, old_bx, old_by; /* old joystick coordinates   */
  170. byte   bstat;                          /* button status              */
  171. byte   old_bstat = 0;                  /* old button status          */
  172. int    key = 0;                        /* keystroke                  */
  173. int    gport = 1;                      /* gport mode flag            */
  174. union  REGS regs;                      /* CPU registers for bios     */
  175. char   *mode[] = { "Bios", "Gport" };  /* mode text                  */
  176. char   *cmode[] = { "Real", "Mean" };
  177. byte   center = 0;
  178.  
  179.  
  180.    Init();
  181.    old_ax = old_ay = old_bx = old_by =0x7fff;
  182.  
  183.  
  184.    while(key != ESC)
  185.    {
  186.  
  187.          /* If we are not in Gport mode, call the bios to */
  188.          /* get the joystick positions.                   */
  189.  
  190.       if(!gport)
  191.       {
  192.          regs.h.ah = 0x84;
  193.          regs.x.dx = 1;
  194.          int86(0x15, ®s, ®s);
  195.          ax = ( regs.x.ax - bios_ax0) * BIOS_GAIN;
  196.          ay = (-regs.x.bx + bios_ay0) * BIOS_GAIN;
  197.          bx = ( regs.x.cx - bios_bx0) * BIOS_GAIN;
  198.          by = (-regs.x.dx + bios_by0) * BIOS_GAIN;
  199.  
  200.          ax = limit(ax,1000,-1000);
  201.          ay = limit(ay,1000,-1000);
  202.          bx = limit(bx,1000,-1000);
  203.          by = limit(by,1000,-1000);
  204.  
  205.       }
  206.  
  207.  
  208.          /* If we are in Gport mode, call GamAxis or GamStick to */
  209.          /* get the joystick position for joystick A             */
  210.  
  211.       if(gport)
  212.       {
  213. #if AXIS
  214.          ax = GamAxis(JAX);
  215.          ay = GamAxis(JAY);
  216. #else
  217.          GamAxes(JA, &ax, &ay);
  218. #endif
  219.       }
  220.  
  221.  
  222.          /* If either coordinate has changed since the last time  */
  223.          /* it was checked, the screen must be updated with the   */
  224.          /* new value                                             */
  225.  
  226.       if(ax != old_ax)
  227.       {
  228.          PutCoord(OAX, JAY_COORD, textwidth("  +0000"), ax);
  229.          old_ax = ax;
  230.       }
  231.       if(ay != old_ay)
  232.       {
  233.          PutCoord(OAX, JAY_COORD, - textwidth("Y = "), ay);
  234.          old_ay = ay;
  235.       }
  236.  
  237.  
  238.          /* Apply the gain divisor to the new coordinates and  */
  239.          /* update the cross hair.                             */
  240.  
  241.       ax /= GAIN; ay /= GAIN;
  242.       CrossHair(OAX + ax, OAY - ay);
  243.  
  244.  
  245.          /* If we are in Gport mode, call GamAxis or GamStick to */
  246.          /* get the joystick position for joystick B             */
  247.  
  248.       if(gport)
  249.       {
  250. #if AXIS
  251.          bx = GamAxis(JBX);
  252.          by = GamAxis(JBY);
  253. #else
  254.          GamAxes(JB, &bx, &by);
  255. #endif
  256.       }
  257.  
  258.  
  259.          /* If either coordinate has changed since the last time  */
  260.          /* it was checked, the screen must be updated with the   */
  261.          /* new value                                             */
  262.  
  263.       if(bx != old_bx)
  264.       {
  265.          PutCoord(OBX, JBY_COORD, textwidth("  +0000") , bx);
  266.          old_bx = bx;
  267.       }
  268.       if(by != old_by)
  269.       {
  270.          PutCoord(OBX, JBY_COORD, - textwidth("Y = "), by);
  271.          old_by = by;
  272.       }
  273.  
  274.  
  275.          /* Apply the gain divisor to the new coordinates and  */
  276.          /* update the cross hair.                             */
  277.  
  278.       bx /= 7; by /= GAIN;
  279.       CrossHair(OBX + bx, OBY - by);
  280.  
  281.  
  282.          /* If we are in Gport mode, use GamButton. Otherwise */
  283.          /* make the call to the bios.                        */
  284.  
  285.       if(gport)
  286.